<?php

  /**
   * 
   */
  class loginController extends Controller
  {

	  public function actionIndex()
	  {

		  $this->layout = '//layouts/blank';

		  $model = new LoginForm;

		  // if it is ajax validation request
		  if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form')
		  {
			  echo CActiveForm::validate($model);
			  Yii::app()->end();
		  }

		  // collect user input data
		  if (isset($_POST['LoginForm']))
		  {
			  $model->attributes = $_POST['LoginForm'];
			  if ($model->validate())
			  {
//				$role = Users::model()->find("userEmail = '$model->username'")->userGroup;
//				if ($role != 'admin')
//					throw new CHttpException(404, 'Error');
				  // validate user input and redirect to the previous page if valid
				  if ($model->login())
				  {
                                        $this->redirect( ( Yii::app()->user->returnUrl == '') ? '/x8' : Yii::app()->user->returnUrl);
				  }
			  }
		  }
		  // display the login form
		  $this->render('index', array('model' => $model));
	  }

	  /**
	   * Logs out the current user and redirect to homepage.
	   */
	  public function actionLogout()
	  {
		  Yii::app()->user->logout();
		  $this->redirect(Yii::app()->homeUrl);
	  }

  }
  